// ColorDlg.cpp : implementation file
//

#include "stdafx.h"
#include "frame_top.h"
#include "DlgColor.h"
#include "Page_Main.h"
#include <utility/api_tools.h>
#include <utility/ini_op.h>
#include <ui/setdpi.h>
// dlg_color dialog

IMPLEMENT_DYNAMIC(dlg_color, CDialog)

dlg_color::dlg_color(CWnd* pParent /*=NULL*/)
	: CDialog(dlg_color::IDD, pParent)
	, m_nHue(0)
	, m_nSat(0)
{
	
}

dlg_color::~dlg_color()
{
}

void dlg_color::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Slider(pDX, IDC_SLIDER_HUE, m_nHue);
	DDX_Slider(pDX, IDC_SLIDER_SAT, m_nSat);
	DDX_Control(pDX, IDC_SLIDER_HUE, m_ctrlSliderHue);
	DDX_Control(pDX, IDC_SLIDER_SAT, m_ctrlSliderSat);
}


BEGIN_MESSAGE_MAP(dlg_color, CDialog)
	ON_BN_CLICKED(IDC_RADIO_COLOR, &dlg_color::OnBnClickedRadioColor)
	ON_BN_CLICKED(IDC_RADIO_SHADING, &dlg_color::OnBnClickedRadioShading)
	ON_WM_NCACTIVATE()
	ON_WM_HSCROLL()
END_MESSAGE_MAP()


// dlg_color message handlers

BOOL dlg_color::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  Add extra initialization here
	CSetDPI dpi;
	dpi.Attach(AfxFindResourceHandle(MAKEINTRESOURCE(IDD), RT_DIALOG),
		m_hWnd,IDD,96.0); 

	CreateColorBtn();
	CreateShadingBtn();
	CButton* pBtn = (CButton*)GetDlgItem( IDC_RADIO_COLOR);
	pBtn->SetCheck(BST_CHECKED);
	OnBnClickedRadioColor();

	dsSkinWindow(GetSafeHwnd(), SKIN_TYPE_CHILDDIALOG, "colordialog", FALSE);
	dsSkinWindow(GetDlgItem(IDC_RADIO_COLOR)->GetSafeHwnd(), SKIN_TYPE_RADIOBUTTON, _T("colorTabButton"), FALSE);
	dsSkinWindow(GetDlgItem(IDC_RADIO_SHADING)->GetSafeHwnd(), SKIN_TYPE_RADIOBUTTON, _T("colorTabButton"), FALSE);

	dsSkinWindow(GetDlgItem(IDC_SLIDER_HUE)->GetSafeHwnd(), SKIN_TYPE_SLIDER, _T("hue_sliderctrl"), FALSE);
	dsSkinWindow(GetDlgItem(IDC_SLIDER_SAT)->GetSafeHwnd(), SKIN_TYPE_SLIDER, _T("sat_sliderctrl"), FALSE);
	dsSkinWindow(GetDlgItem(IDC_STATIC_HUE)->GetSafeHwnd(), SKIN_TYPE_STATIC, _T("static"), FALSE);
	dsSkinWindow(GetDlgItem(IDC_STATIC_SAT)->GetSafeHwnd(), SKIN_TYPE_STATIC, _T("static"), FALSE);

	int sat, hue;
	aot::tt::command_t cmd;
	aot::tt::result_t r;
	cmd.type = WM_GLB_UI_GET_UI_CONFIG;
	cmd.val = aot::e_ui_config_change_colorhue;
	dll_object::im_->command_to("imain", &cmd, &r);
	hue = r.ret;
	cmd.type = WM_GLB_UI_GET_UI_CONFIG;
	cmd.val = aot::e_ui_config_change_colorsat;
	dll_object::im_->command_to("imain", &cmd, &r);
	sat = r.ret;

	m_ctrlSliderHue.SetRange(-180, 180, TRUE);
	m_ctrlSliderHue.SetTic(1);
	m_ctrlSliderHue.SetPos(hue);
	m_ctrlSliderHue.SetPageSize(1);

	m_ctrlSliderSat.SetRange(50, 150, TRUE);
	m_ctrlSliderSat.SetTic(1);
	m_ctrlSliderSat.SetPos(sat);
	m_ctrlSliderSat.SetTicFreq(10);
	m_ctrlSliderSat.SetPageSize(1);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void dlg_color::CreateColorBtn()
{
	// Create a color button.
	int nLeft = 14;
	int nTop = 45;

	for ( int i = 0 ; i < sizeof(m_btnColor)/sizeof(CButton); i++)
	{
		int nTopValue = nTop ;
		int nLeftValue = nLeft;

		nTopValue = nTop + (i / 4) * 30;
		nLeftValue = nLeft + ( i % 4) * 30;

		m_btnColor[i].Create(_T(""), WS_CHILD|BS_PUSHBUTTON, CRect(nLeftValue,nTopValue,nLeftValue+20,nTopValue+20), this, 100+i);
		CString str;
		str.Format( _T("colorBtn_%d"), i);
		dsSkinWindow(m_btnColor[i].GetSafeHwnd(), SKIN_TYPE_BUTTON, str, FALSE);
	}

}

void dlg_color::CreateShadingBtn()
{
	int nLeft = 12;
	int nTop = 41;
	for ( int i = 0 ; i < sizeof(m_btnShading)/sizeof(CButton); i++)
	{
		int nTopValue = nTop ;
		int nLeftValue = nLeft;
		nTopValue = nTop + (i / 3) * 43;
		nLeftValue = nLeft + (i % 3) * 42;
		m_btnShading[i].Create(_T(""), WS_CHILD|BS_PUSHBUTTON, CRect(nLeftValue,nTopValue,nLeftValue+31,nTopValue+31), this, 200+i);
		CString str;
		str.Format(_T("texture_%d"), i);
		dsSkinWindow(m_btnShading[i].GetSafeHwnd(), SKIN_TYPE_BUTTON, str, FALSE);
	}
}

void dlg_color::OnBnClickedRadioColor()
{
	// TODO: Add your control notification handler code here
	for ( int i = 0 ; i < sizeof(m_btnColor)/sizeof(CButton); i++)
	{
		m_btnColor[i].ShowWindow( SW_SHOW);
	}

	for ( int i = 0 ; i < sizeof(m_btnShading)/sizeof(CButton); i++)
	{
		m_btnShading[i].ShowWindow( SW_HIDE);
	}

	GetDlgItem(IDC_SLIDER_HUE)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_SLIDER_SAT)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_STATIC_HUE)->ShowWindow(SW_SHOW);
	GetDlgItem(IDC_STATIC_SAT)->ShowWindow(SW_SHOW);
}

void dlg_color::OnBnClickedRadioShading()
{
	// TODO: Add your control notification handler code here
	for ( int i = 0 ; i < sizeof(m_btnColor)/sizeof(CButton); i++)
	{
		m_btnColor[i].ShowWindow( SW_HIDE);
	}

	for ( int i = 0 ; i < sizeof(m_btnShading)/sizeof(CButton); i++)
	{
		m_btnShading[i].ShowWindow( SW_SHOW);
	}
	
	GetDlgItem(IDC_SLIDER_HUE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_SLIDER_SAT)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_HUE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_STATIC_SAT)->ShowWindow(SW_HIDE);
}

BOOL dlg_color::OnNcActivate(BOOL bActive)
{
	// TODO: Add your message handler code here and/or call default
	aot_log_debug(AOT_LM_DEBUG, "%s", "hide color dlg");
	ShowWindow(SW_HIDE);
	return CDialog::OnNcActivate(bActive);
}

BOOL dlg_color::OnCommand(WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your specialized code here and/or call the base class

	int nValue = LOWORD( wParam);
	int nHue, nSat;
	switch ( nValue )
	{
	case 100:
		nHue = 0;
		nSat = 100;
		break;

	case 101:
		nHue = -5;
		nSat = 119;
		break;

	case 102:
		nHue = 59;
		nSat = 100;
		break;
	case 103:
		nHue = 120;
		nSat = 115;
		break;
	case 104:
		nHue = 159;
		nSat = 116;
		break;
	case 105:
		nHue = -159;
		nSat = 111;
		break;
	case 106:
		nHue = -69;
		nSat = 97;
		break;
	case 107:
		nHue = 0;
		nSat = 0;
		break;
	case 200:
		nHue = 0;
		nSat = 100;
		break;
	case 201:
		nHue = -61;
		nSat = 68;
		break;
	case 202:
		nHue = 148;
		nSat = 97;
		break;
	case 203:
		nHue = 0;
		nSat = 0;
		break;
	case 204:
		nHue = 138;
		nSat = 97;
		break;
	case 205:
		nHue = -174;
		nSat = 124;
		break;
	case 206:
		nHue = 26;
		nSat = 50;
		break;
	case 207:
		nHue = 6;
		nSat = 60;
		break;
	case 208:
			nHue = -74;
			nSat = 65;
		break;
	default:
		break;
	}
	
	if ( nValue >= 200 && nValue <= 208)
	{
		set_theme(nValue);
		set_color(nSat, nHue);
		CPage_Main* pDlg = (CPage_Main*)GetParent();
		pDlg->SetShadingItem(nValue);

		if ( dll_object::im_ )
		{
			aot::tt::command_t cmd;
			aot::tt::result_t r;
			cmd.type = WM_GLB_UI_CHANGE_SHADING;
			cmd.val = nValue;
			dll_object::im_->command_to("idll.imchat.dll", &cmd, &r);
		}
		
		dsAdjustColor(nHue, nSat, 0);

		m_nHue = nHue;
		m_nSat = nSat;
		UpdateData(FALSE);
		return TRUE;
	
	}


	if ( nValue >= 100 && nValue <= 107 )
	{
		set_theme(nValue);
		set_color(nSat, nHue);
		dsAdjustColor(nHue, nSat, 0);

		m_nHue = nHue;
		m_nSat = nSat;
		UpdateData(FALSE);
	}

	return CDialog::OnCommand(wParam, lParam);
}

void dlg_color::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	if ( GetDlgItem( IDC_SLIDER_HUE) == pScrollBar && nSBCode == SB_ENDSCROLL)
	{

		UpdateData( TRUE);
		dsAdjustColor(m_nHue, m_nSat, 0);
	}
	else if ( GetDlgItem( IDC_SLIDER_SAT) == pScrollBar && nSBCode == SB_ENDSCROLL )
	{
		UpdateData( TRUE);
		dsAdjustColor(m_nHue, m_nSat, 0);
	}

	TRACE(_T("the hue[%d] sta[%d]"), m_nHue, m_nSat);
	set_color(m_nSat, m_nHue);
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

BOOL dlg_color::set_theme(int themeid)
{
	aot::tt::command_t cmd;
	aot::tt::result_t r;
	cmd.type = WM_GLB_UI_CONFIG_CHANGE;
	cmd.val = aot::e_ui_config_change_themeid;
	cmd.user_data[0] = themeid;
	dll_object::im_->command_to("imain", &cmd, &r);
	return TRUE;
}

BOOL dlg_color::set_color(int sat, int hue)
{
	aot::tt::command_t cmd;
	aot::tt::result_t r;
	cmd.type = WM_GLB_UI_CONFIG_CHANGE;
	cmd.val = aot::e_ui_config_change_colorhue;
	cmd.user_data[0] = hue;
	dll_object::im_->command_to("imain", &cmd, &r);

	cmd.type = WM_GLB_UI_CONFIG_CHANGE;
	cmd.val = aot::e_ui_config_change_colorsat;
	cmd.user_data[0] = sat;
	dll_object::im_->command_to("imain", &cmd, &r);
	return TRUE;
}
